package com.line.myprojectservice.chatserver.initializer;

import com.line.myprojectservice.chatserver.handler.DispatchHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Line
 */
@Slf4j
public class MyChanneInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        //Http编解码器
        pipeline.addLast(new HttpServerCodec());
        //支持异步发送大的码流，一般用于发送文件流
        pipeline.addLast(new ChunkedWriteHandler());
        //将多个消息转换成单一的消息对象
        pipeline.addLast(new HttpObjectAggregator(64 * 1024));

        pipeline.addLast("Dispatcher",new DispatchHandler());


        //按照 WebSocket 规范的要求，处理 WebSocket 升级握手、
        //PingWebSocketFrame 、 PongWebSocketFrame 和 CloseWebSocketFrame
        //协议升级 ，这关于参数问题 需要调一下
        //pipeline.addLast(new WebSocketServerProtocolHandler("/ws", null, true, 65536 * 10));

        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));

    }

}

